<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-device:device:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-device:device:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-device:device:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-device:device:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="设备名称"
        align="center"
        prop="name"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="类型名称" align="center" prop="typeName" />
      <el-table-column
        label="设备型号"
        align="center"
        prop="model"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="设备编码"
        align="center"
        prop="code"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="制造商" align="center" prop="manufacturer" />
      <el-table-column
        label="位置"
        align="center"
        prop="location"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="购买日期"
        align="center"
        prop="buyDate"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.buyDate, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备状态" align="center" prop="stateResult">
        <template #default="scope">
          <div :style="scope.row.stateStyle">{{ scope.row.stateResult }}</div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-device:device:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-device:device:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deviceRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="typeId">
          <el-tree-select
            v-model="form.typeId"
            :data="optionsDevice"
            :props="{
              value: 'id',
              label: 'typeName',
              children: 'children',
            }"
            placeholder="选择设备类型"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="单位" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="optionsDept"
            :props="{
              value: 'deptId',
              label: 'deptName',
              children: 'children',
            }"
            placeholder="选择单位"
            check-strictly
          />
        </el-form-item>
        <!-- <el-form-item label="设备类型id" prop="typeId">
          <el-input v-model="form.typeId" placeholder="请输入设备类型id" />
        </el-form-item> -->
        <!-- <el-form-item label="工单类型id" prop="wId">
          <el-input v-model="form.wId" placeholder="请输入工单类型id" />
        </el-form-item> -->

        <el-form-item label="设备型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入设备型号" />
        </el-form-item>
        <el-form-item label="设备编码" prop="code">
          <el-input v-model="form.code" placeholder="请输入设备编码" />
        </el-form-item>
        <!-- <el-form-item label="使用人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入使用人" />
        </el-form-item> -->
        <!-- <el-form-item label="使用人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入使用人id" />
        </el-form-item> -->
        <el-form-item label="RFID卡号" prop="rfid">
          <el-input v-model="form.rfid" placeholder="请输入RFID卡号" />
        </el-form-item>
        <!-- <el-form-item label="二维码" prop="qrcode">
          <el-input
            v-model="form.qrcode"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item> -->
        <el-form-item label="设备描述" prop="deviceInfo">
          <el-input v-model="form.deviceInfo" placeholder="请输入设备描述" />
        </el-form-item>
        <el-form-item label="使用说明" prop="userInfo">
          <el-input v-model="form.userInfo" placeholder="请输入使用说明" />
        </el-form-item>
        <el-form-item label="制造商" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入制造商" />
        </el-form-item>
        <el-form-item label="位置" prop="location">
          <el-input v-model="form.location" placeholder="请输入位置" />
        </el-form-item>
        <el-form-item label="购买日期" prop="buyDate">
          <el-date-picker
            clearable
            v-model="form.buyDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择购买日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="凭证号" prop="voucherNo">
          <el-input v-model="form.voucherNo" placeholder="请输入凭证号" />
        </el-form-item>
        <el-form-item label="设备状态" prop="state">
          <el-select
            v-model="form.state"
            placeholder="请选择设备状态"
            filterable
          >
            <el-option
              v-for="item in optionsIfCheck"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Device">
import { listDeviceClass } from "@/api/ruoyi-device/deviceClass";

const optionsDevice = ref([]);
function getTreeselectDevice() {
  optionsDevice.value = [];
  let params1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listDeviceClass(params1).then((response) => {
    optionsDevice.value = proxy.handleTree(response.rows, "id", "pid");
  });
}

const optionsIfCheck = ref([
  {
    label: "正常",
    value: 0,
  },
  {
    label: "在用",
    value: 1,
  },
  {
    label: "不可用",
    value: 2,
  },
]);

onMounted(() => {
  getTreeselectDept();
  getTreeselectDevice();
});

import { listDept } from "@/api/system/dept";
const optionsDept = ref([]);
const arrayDept = ref([]);
function getTreeselectDept() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listDept(queryParams1).then((response) => {
    optionsDept.value = [];
    arrayDept.value = response.data;
    arrayDept.value.forEach((item1) => {
      item1.deptId = Number(item1.deptId);
    });
    optionsDept.value = proxy.handleTree(arrayDept.value, "deptId");
    console.log("树", optionsDept.value);
    getList();
  });
}

import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice,
} from "@/api/ruoyi-device/device";
import { onMounted } from "vue";

const { proxy } = getCurrentInstance();

const deviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询设备管理列表 */
function getList() {
  loading.value = true;
  listDevice(queryParams.value).then((response) => {
    console.log(response.rows);
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    deviceList.value.forEach((item1) => {
      if (item1.state == 0) {
        item1.stateResult = "正常";
        item1.stateStyle = {
          color: "green",
        };
      } else if (item1.state == 1) {
        item1.stateResult = "在用";
        item1.stateStyle = {
          color: "blue",
        };
      } else if (item1.state == 2) {
        item1.stateResult = "不可用";
        item1.stateStyle = {
          color: "red",
        };
      }
    });

    console.log(deviceList.value);
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deptId: null,
    typeId: null,
    wId: null,
    name: null,
    model: null,
    code: null,
    userName: null,
    userId: null,
    rfid: null,
    qrcode: null,
    deviceInfo: null,
    userInfo: null,
    manufacturer: null,
    enlarge: null,
    location: null,
    buyDate: null,
    picture: null,
    voucherNo: null,
    state: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    delFlag: null,
  };
  proxy.resetForm("deviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getDevice(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备管理";
  });
}

/** 提交按钮 */
function submitForm() {
  delete (form.value, "typeName");
  proxy.$refs["deviceRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateDevice(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDevice(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除设备管理编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delDevice(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/device/export",
    {
      ...queryParams.value,
    },
    `device_${new Date().getTime()}.xlsx`
  );
}
</script>
